package com.modbus.tcp.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.modbus.tcp.utils.ApiReturnObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.modbus.tcp.utils.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.modbus.tcp.domain.Fiberhostpart;
import com.modbus.tcp.service.IFiberhostpartService;
/**
 * 光纤测温主机分区数据 信息操作处理
 *
 * @author d
 * @date 2024-01-17
 */
@RestController
@RequestMapping("/fiberhostpart")
public class FiberhostpartController  {

    @Autowired
    private IFiberhostpartService fiberhostpartService;

    /**
     * 查询光纤测温主机分区数据列表
     */
    @GetMapping("/list")
    public ApiReturnObject list(Fiberhostpart fiberhostpart,Integer pageNum,Integer pageSize) {
        if(pageNum != null && pageSize !=null ){
            PageHelper.startPage(pageNum,pageSize);
        }
        List<Fiberhostpart> list = fiberhostpartService.selectFiberhostpartList(fiberhostpart);
        PageInfo pageInfo = new PageInfo<>(list);
        return new ApiReturnObject("200","查询成功",pageInfo);
    }

    @GetMapping("/selectEchartsData")
    public ApiReturnObject selectEchartsData( Integer id) {
        List<Fiberhostpart> list = fiberhostpartService.selectEchartsData(id);
        List<Map> list2 = new ArrayList<>();


        for(Fiberhostpart fiberhostpart:list){

            Map<String,Object> map = new HashMap<>();
            List<String>  legenddata = new ArrayList<>();
            List<String> xAxisdata = new ArrayList<>();
            List<Float>  seriesdata = new ArrayList<>();

            legenddata.add("通道"+fiberhostpart.getChannelid().toString());
            String[] seriesarray = fiberhostpart.getSeriesdata().split(",");
            for(String series:seriesarray){
                seriesdata.add(Convert.toFloat(series));
            }
            String[] xaxisarray = fiberhostpart.getXAxisdata().split(",");
            for(String series:xaxisarray){
                xAxisdata.add(series);
            }


//            seriesdata.add(fiberhostpart.getSeriesdata());
//            xAxisdata.add(fiberhostpart.getXAxisdata());
            map.put("legenddata",legenddata);
            map.put("xAxisdata",xAxisdata);
            map.put("seriesdata",seriesdata);
            list2.add(map);
        }


        return new ApiReturnObject("200","查询成功",list2);
    }
    /**
     * 新增保存光纤测温主机分区数据
     */

    @PostMapping("/add")
    public ApiReturnObject addSave(@RequestBody Fiberhostpart fiberhostpart) {
        fiberhostpartService.insertFiberhostpart(fiberhostpart);
        return new ApiReturnObject("200","添加成功","");
    }


    /**
    * 详情光纤测温主机分区数据
    */
    @GetMapping("/selectOne")
    public ApiReturnObject selectOne(Integer id){
        Fiberhostpart result = fiberhostpartService.selectFiberhostpartById(id);
        return new ApiReturnObject("200","查询成功",result);
    }

    /**
     * 修改保存光纤测温主机分区数据
     */

    @PostMapping("/update")
    public ApiReturnObject editSave(@RequestBody Fiberhostpart fiberhostpart) {
        fiberhostpartService.updateFiberhostpart(fiberhostpart);
        return new ApiReturnObject("200","修改成功","");
    }

    /**
     * 删除光纤测温主机分区数据
     */

    @PostMapping("/del")
    public ApiReturnObject remove(String ids) {
        fiberhostpartService.deleteFiberhostpartByIds(ids);
        return new ApiReturnObject("200","删除成功","");
    }
}